#include "GameObject.h"

using namespace GalliumEngine;

GameObject::GameObject(void)
{

}

GameObject::~GameObject(void)
{

}

void GameObject::setId(std::string _id)
{
	m_id = _id;
}

std::string GameObject::getId()
{
	return m_id;
}

bool GameObject::addBehaviourComponent(BehaviourComponent* _c)
{
	BehaviourComponentConstItr itr = m_behaviourComponents.find(_c->getId());

	if(itr == m_behaviourComponents.end())
	{
		m_behaviourComponents.insert(BehaviourComponentPair(_c->getId(),_c));

		return true;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: BehaviourComponent Already Exists! \n addBehaviourComponent(BehaviourComponent* c) failed to add the BehaviourComponent* %s in GameObject %s.",
			_c->getId(),
			getId()));

		return false;
	}
}

bool GameObject::removeBehaviourComponent(std::string _s)
{
	BehaviourComponentConstItr itr = m_behaviourComponents.find(_s);

	if(itr != m_behaviourComponents.end())
	{
		m_behaviourComponents.erase(itr);

		itr->second->deinitialize();

		delete itr->second;

		return true;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: BehaviourComponent Not Found! \n removeBehaviourComponent(std::string*) failed to removed the BehaviourComponent* %s in GameObject %s.",
			_s,
			getId()));

		return false;
	}
}

BehaviourComponent* GameObject::getBehaviourComponent(std::string _s)
{
	BehaviourComponentConstItr itr = m_behaviourComponents.find(_s);

	if(itr != m_behaviourComponents.end())
	{
		return itr->second;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: BehaviourComponent Does not Exist! \n getBehaviourComponent(std::string) failed find the requested BehaviourComponent* %s in GameObject %s.",
			_s,
			getId()));

		return NULL;
	}
}

bool GameObject::addAttributeComponent(AttributeComponent* _c)
{
	AttributeComponentConstItr itr = m_attributeComponents.find(_c->getId());

	if(itr == m_attributeComponents.end())
	{
		m_attributeComponents.insert(AttributeComponentPair(_c->getId(),_c));

		return true;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: AttributeComponent Already Exists! \n addAttributeComponent(AttributeComponent* c) failed to add the AttributeComponent* %s in GameObject %s.",
			_c->getId(),
			getId()));

		return false;
	}
}

bool GameObject::removeAttributeComponent(std::string _s)
{
	AttributeComponentConstItr itr = m_attributeComponents.find(_s);

	if(itr != m_attributeComponents.end())
	{
		m_attributeComponents.erase(itr);

		itr->second->deinitialize();

		delete itr->second;

		return true;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: AttributeComponent Not Found! \n removeAttributeComponent(std::string*) failed to removed the AttributeComponent* %s in GameObject %s.",
			_s,
			getId()));

		return false;
	}
}

AttributeComponent* GameObject::getAttributeComponent(std::string _s)
{
	AttributeComponentConstItr itr = m_attributeComponents.find(_s);

	if(itr != m_attributeComponents.end())
	{
		return itr->second;
	}
	else
	{
		Logger::getInstance()->write(
			StringUtils::format(
			"ERROR: AttributeComponent Does not Exist! \n getAttributeComponent(std::string) failed find the requested AttributeComponent* %s in GameObject %s.",
			_s,
			getId()));

		return NULL;
	}
}

void GameObject::initialize()
{
	/*for (ComponentConstItr itr = m_components.begin(); itr != m_components.end(); ++itr)
	{
		itr->second->initialize();
	}*/
}

void GameObject::deinitialize()
{
	/*for (ComponentConstItr itr = m_components.begin(); itr != m_components.end(); ++itr)
	{
		itr->second->deInitialize();
	}*/
}

void GameObject::update(float dt)
{
	/*for (ComponentConstItr itr = m_components.begin(); itr != m_components.end(); ++itr)
	{
		
	}*/
}

void GameObject::render()
{
	/*for (ComponentConstItr itr = m_components.begin(); itr != m_components.end(); ++itr)
	{
		if(itr->second->getFamily() == "DRAWABLE")
		{
			itr->second->draw();
		}
	}*/
}